﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
//using System.Windows.Forms;
using InkFx.WinControl.Core;
using InkFx.WinControl.Design;
using InkFx.WinControl.Theme;
using InkFx.WinControl.Utils;

namespace InkFx.WinControl
{
    /// <summary>
    /// 表示允许用户调整停靠控件大小的拆分器控件。System.Windows.Forms.Splitter 已被 System.Windows.Forms.SplitContainer 取代，且仅为与早期版本兼容而提供。
    /// </summary>
    //[Designer(typeof(SplitterDesigner))]
    [ToolboxItem(true), ToolboxBitmap(typeof(Splitter), "Resources.Toolbox.Splitter.png")]
    //[TechSupport("http://ink-fx.com/SDK/InkFx.WinControl/Splitter/")]
    public class Splitter : System.Windows.Forms.Splitter
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public Splitter()
        {
            SetStyle(Win32API.ControlStylesUAORSD, true);

            //m_LastSplitterDistance = base.SplitterDistance;
        }


        #region  字 段 属 性

        /// <summary>
        /// 内部字段
        /// </summary>
        protected Splitter.SplitPartType m_MouseHoverRectType = Splitter.SplitPartType.None;
        /// <summary>
        /// 内部字段
        /// </summary>
        protected Splitter.SplitPartType m_MouseDownRectType = Splitter.SplitPartType.None;


        /// <summary>
        /// 
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Always)]
        public System.Windows.Forms.Orientation Orientation
        {
            get { return (this.Dock == System.Windows.Forms.DockStyle.Top || this.Dock == System.Windows.Forms.DockStyle.Bottom) ? System.Windows.Forms.Orientation.Horizontal : System.Windows.Forms.Orientation.Vertical; }
        }

        #endregion


        #region  函 数 改 写

        /// <summary>
        /// 引发 System.Windows.Forms.Control.Paint 事件。
        /// </summary>
        /// <param name="e">包含事件数据的 System.Windows.Forms.PaintEventArgs。</param>
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            try
            {
                base.OnPaint(e);
                this.ResetRect();
            }
            catch (Exception ex) { Tools.LogWarn2(ex, "改写 Splitter.OnPaint(PaintEventArgs e) 发生错误"); }
        }

        /// <summary>
        /// 引发 System.Windows.Forms.Control.MouseMove 事件。
        /// </summary>
        /// <param name="e">包含事件数据的 System.Windows.Forms.MouseEventArgs。</param>
        protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button != System.Windows.Forms.MouseButtons.None)
            {
                //如果鼠标在按下状态, 即为 拖拽 状态
                m_MouseHoverRectType = Splitter.SplitPartType.None; //拖拽状态时, 无悬浮状态
                Rectangle rectSpliter = RectSpliter;
                this.Invalidate(rectSpliter);
                base.OnMouseMove(e);
            }
            else
            {
                Point ctrlPoint = e.Location;
                //Point ctrlPoint = Win32API.GetMouseCtrlPoint(this);
                Rectangle rectSpliter = RectSpliter;

                if (rectSpliter.Contains(ctrlPoint))
                {
                    //SetCursor(Cursors.Default);
                    System.Windows.Forms.Cursor cursor = (this.Orientation == System.Windows.Forms.Orientation.Horizontal) ? System.Windows.Forms.Cursors.HSplit : System.Windows.Forms.Cursors.VSplit;
                    SetCursor(cursor);
                    m_MouseHoverRectType = Splitter.SplitPartType.Spliter;
                    base.OnMouseMove(e);
                }
                else
                {
                    SetCursor(System.Windows.Forms.Cursors.Default);
                    m_MouseHoverRectType = Splitter.SplitPartType.None;
                    base.OnMouseMove(e);
                }

                this.Invalidate(rectSpliter);
            }

        }
        /// <summary>
        /// 引发 System.Windows.Forms.Control.MouseLeave 事件。
        /// </summary>
        /// <param name="e">包含事件数据的 System.EventArgs。</param>
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);

            SetCursor(System.Windows.Forms.Cursors.Default);
            if (MouseButtons == System.Windows.Forms.MouseButtons.None)
            {
                m_MouseHoverRectType = Splitter.SplitPartType.None;
                Rectangle rectSpliter = RectSpliter;
                this.Invalidate(rectSpliter);
            }
        }
        /// <summary>
        /// 引发 System.Windows.Forms.Control.MouseDown 事件。
        /// </summary>
        /// <param name="e">包含事件数据的 System.Windows.Forms.MouseEventArgs。</param>
        protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            Rectangle rectSpliter = RectSpliter;
            Point ctrlPoint = e.Location;

            if (rectSpliter.Contains(ctrlPoint))
            {
                m_MouseDownRectType = Splitter.SplitPartType.Spliter;
                base.OnMouseDown(e);
            }
            else
            {
                m_MouseDownRectType = Splitter.SplitPartType.None;
                base.OnMouseDown(e);
            }

            this.Invalidate(rectSpliter);
        }
        /// <summary>
        /// 引发 System.Windows.Forms.Control.MouseUp 事件。
        /// </summary>
        /// <param name="e">包含事件数据的 System.Windows.Forms.MouseEventArgs。</param>
        protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
        {
            base.OnMouseUp(e);

            Rectangle rectSpliter = RectSpliter;
            Point ctrlPoint = e.Location;
            //Point ctrlPoint = Win32API.GetMouseCtrlPoint(this);

            ////触发事件
            //if (m_ExpandPanel != Splitter.ExpandPanel.None && mouseDownRectType == Splitter.SplitPartType.Button && e.Button == MouseButtons.Left && rectButton.Contains(ctrlPoint))
            //    OnButtonClick(EventArgs.Empty);

            m_MouseDownRectType = Splitter.SplitPartType.None;
            this.Invalidate(rectSpliter);
        }
        /// <summary>
        /// 引发 System.Windows.Forms.Control.KeyUp 事件。
        /// </summary>
        /// <param name="e">包含事件数据的 System.Windows.Forms.KeyEventArgs。</param>
        protected override void OnKeyUp(System.Windows.Forms.KeyEventArgs e)
        {
            base.OnKeyUp(e);
            Rectangle rectSpliter = RectSpliter;
            this.Invalidate(rectSpliter);
        }


        /// <summary>
        /// 引发 System.Windows.Forms.Control.HandleCreated 事件。
        /// </summary>
        /// <param name="e">包含事件数据的 System.EventArgs。</param>
        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
        }
        /// <summary>
        /// 释放由 System.Windows.Forms.Control 和它的子控件占用的非托管资源，另外还可以释放托管资源。
        /// </summary>
        /// <param name="disposing">为 true 则释放托管资源和非托管资源；为 false 则仅释放非托管资源。</param>
        protected override void Dispose(bool disposing)
        {
            try
            {
                base.Dispose(disposing);
            }
            catch (Exception ex) { Tools.LogWarn2(ex, "可忽略的偶发错误"); }
        }
        /// <summary>
        /// 引发 System.Windows.Forms.Control.GotFocus 事件。
        /// </summary>
        /// <param name="e">包含事件数据的 System.EventArgs。</param>
        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
        }
        /// <summary>
        /// 引发 System.Windows.Forms.Control.LostFocus 事件。
        /// </summary>
        /// <param name="e">包含事件数据的 System.EventArgs。</param>
        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);
        }

        private void SetCursor(System.Windows.Forms.Cursor cursor)
        {
            System.Windows.Forms.Cursor baseCursor = base.Cursor;
            if (baseCursor != null && baseCursor != cursor)
                base.Cursor = cursor;
        }

        #endregion


        #region  控 件 区 域


        /// <summary>
        /// 内部字段
        /// </summary>
        protected Rectangle m_FullRect = Rectangle.Empty;
        /// <summary>
        /// 内部字段
        /// </summary>
        protected Rectangle m_RectHand1 = Rectangle.Empty;
        /// <summary>
        /// 内部字段
        /// </summary>
        protected Rectangle m_RectHand2 = Rectangle.Empty;
        /// <summary>
        /// 内部字段
        /// </summary>
        protected Rectangle m_RectSpliter = Rectangle.Empty;

        /// <summary>
        /// 
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Always)]
        public Rectangle FullRect
        {
            get { return m_FullRect; }
        }
        /// <summary>
        /// 
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Always)]
        public virtual Rectangle RectHand1
        {
            get { return m_RectHand1; }

        }
        /// <summary>
        /// 
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Always)]
        public virtual Rectangle RectHand2
        {
            get { return m_RectHand2; }
        }
        /// <summary>
        /// 
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Always)]
        public virtual Rectangle RectSpliter
        {
            get { return m_RectSpliter; }
        }

        /// <summary>
        /// 重新计算 各种区域边界
        /// </summary>
        public virtual void ResetRect()
        {
            m_FullRect = new Rectangle(0, 0, this.Width, this.Height); //this.Location, this.Size
            m_RectSpliter = m_FullRect;

            bool isHorizontal = this.Orientation == System.Windows.Forms.Orientation.Horizontal;
            m_RectHand1 = Rectangle.Empty;
            m_RectHand2 = Rectangle.Empty;
        }


        #endregion



        /// <summary>
        /// 
        /// </summary>
        public enum SplitPartType
        {
            /// <summary>
            /// 
            /// </summary>
            None,
            /// <summary>
            /// 
            /// </summary>
            TLButton,
            /// <summary>
            /// 
            /// </summary>
            BRButton,
            /// <summary>
            /// 
            /// </summary>
            Spliter
        }

    }
}
